package com.example.asus_pc.myshop.activity;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus_pc.myshop.R;
import com.example.asus_pc.myshop.adapter.AbsCommonAdapter;
import com.example.asus_pc.myshop.adapter.AbsViewHolder;
import com.example.asus_pc.myshop.bean.VegaBean;
import com.example.asus_pc.myshop.utils.MathUtils;
import com.example.asus_pc.myshop.utils.ScreenShotUtils;
import com.example.asus_pc.myshop.utils.SharedPreUtils;

import java.util.ArrayList;
import java.util.List;

public class ClipsActivity extends AppCompatActivity implements View.OnClickListener{

    ListView lvTotal;

    private List<VegaBean> datas;
    private EditText etHotel;

    private List<VegaBean> totalVegas;
    private AbsCommonAdapter<VegaBean> adapter;
    private double totalPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clips);
        initViews();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.tv_left).setOnClickListener(this);
        findViewById(R.id.tv_right).setOnClickListener(this);

    }

    private void initViews() {
        etHotel = (EditText) findViewById(R.id.et_hotel);
        lvTotal = (ListView) findViewById(R.id.lv_subtotal);
        adapter = new AbsCommonAdapter<VegaBean>(this, R.layout.item_subtotal) {
            @Override
            public void convert(AbsViewHolder helper, VegaBean item, int pos) {
                TextView tvContent = helper.getView(R.id.tv_vega_subtotal);
                if(pos != datas.size()-1){
                    helper.setText(R.id.tv_order,pos+1+"");
                    double total = MathUtils.add(MathUtils.mul(item.getTotalPrice(),item.getTotalNum()),MathUtils.mul(item.getSplitPrice(),item.getSplitNum()));
                    String contentFull ="";
                    String contentSplit ="";
                    if(item.getTotalNum()>0){
                        contentFull = "<u>整装</u>  <font color = '#EE3333'>"+item.getTotalPrice()+"</font>"+"元/斤 共"
                                +"<font color = '#EE3333'>"+item.getTotalNum()+"</font>"+"斤<br/>";
                    }
                    if(item.getSplitNum()>0){
                        contentSplit = "<u>分装</u>  <font color = '#EE3333'>"+item.getSplitPrice()+"</font>"+"元/斤 共"
                                +"<font color = '#EE3333'>"+item.getSplitNum()+"</font>"+"斤<br/>";
                    }

//                    helper.setText(R.id.tv_vega_subtotal,"【"+item.getVegaName()+"】"+"整装"+item.getTotalPrice()+"元/斤 共"+item.getTotalNum()+"斤 分装"+item.getSplitPrice()+"元/斤 共"+item.getSplitNum()+"斤  小计"+total+"元");
                    tvContent.setText(Html.fromHtml("【"+item.getVegaName()+"】"+contentFull+contentSplit+"  小计<strong><font color = '#EE3333'>"+total+"</font></strong>元"));
                }else {
                    helper.setText(R.id.tv_order,"");
                    tvContent.setText(Html.fromHtml("总计"+totalPrice+"元"));
                }
            }
        };
        lvTotal.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String dataStr = SharedPreUtils.getString(SharedPreUtils.VEGA_CONTENT,"");
        datas =new ArrayList<>();
        totalVegas = VegaBean.jsonToList(dataStr);
        totalPrice = 0d;
        for (int i = 0; i < totalVegas.size(); i++) {
            VegaBean bean = totalVegas.get(i);
            if (totalVegas.get(i).getIsSelected()==1) {
                double totalInSingle  = MathUtils.add(MathUtils.mul(bean.getTotalPrice(),bean.getTotalNum()),MathUtils.mul(bean.getSplitPrice(),bean.getSplitNum()));
                if(totalInSingle != 0 ){
                    datas.add(bean);
                    totalPrice = MathUtils.add(totalPrice,totalInSingle);
                }
            }
        }
        datas.add(new VegaBean());
        adapter.addData(datas);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                // TODO: 2018/9/4
                Bitmap bitmap = ScreenShotUtils.getWholeListViewItemsToBitmap(lvTotal);
                ScreenShotUtils.savePic(this,bitmap,etHotel.getText().toString());
                recycleBitmap(bitmap);
                Toast.makeText(this,"图片将保存在/sdcard/image目录下",Toast.LENGTH_SHORT).show();
                break;
        }

    }
    /**
     * 回收bitmap
     *
     * @param bitmap 需要回收的图片
     */
    private void recycleBitmap(Bitmap bitmap) {
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
        System.gc();
    }
}
